package com.wang.lotterysystem.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.wang.lotterysystem.common.pojo.CommonResult;
import com.wang.lotterysystem.controller.enums.ActivityPrizeTiersEnum;
import com.wang.lotterysystem.controller.param.DrawPrizeParam;
import com.wang.lotterysystem.controller.param.ShowWinningRecordsParam;
import com.wang.lotterysystem.controller.result.WinningRecordResult;
import com.wang.lotterysystem.service.DrawPrizeService;
import com.wang.lotterysystem.service.dto.WinningRecordDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Tag(name = "抽奖相关功能接口")
@RestController
@Slf4j
public class DrawPrizeController {

    @Autowired
    private DrawPrizeService drawPrizeService;

    @Operation(summary = "开始抽奖")
    @RequestMapping("/draw-prize")
    public CommonResult<Boolean> drawPrize(@Validated @RequestBody DrawPrizeParam param) throws JsonProcessingException {
        //打印日志
        log.info("draw prize :{}",param);
        //调用Service层的drawPrize方法
        drawPrizeService.drawPrize(param);
        //将结果转为CommonResult并返回
        return CommonResult.success(true);
    }


    @Operation(description ="获取抽奖结果")
    @RequestMapping("/winning-records/show")
    public CommonResult<List<WinningRecordResult>> showWinningRecords(@Validated @RequestBody ShowWinningRecordsParam param) throws JsonProcessingException {
        //打印日志
        log.info("winning record param :{}",param);
        //调用Service层的getRecord方法
        List<WinningRecordDTO> record = drawPrizeService.getRecord(param);
        //将结果转为CommonResult并返回
        List<WinningRecordResult> results = convertToWinningRecordResult(record);
        return CommonResult.success(results);
    }

    /**
     * 转换结果
     * @param record 查询到的结果
     * @return 返回Controller层需要的结果类型
     */
    private List<WinningRecordResult> convertToWinningRecordResult(List<WinningRecordDTO> record) {
        if (record.isEmpty()){
            log.warn("获取到的活动记录为空");
            return Arrays.asList();
        }
        List<WinningRecordResult> winningRecordResults = new ArrayList<>();
        for (WinningRecordDTO winningRecordDTO :record) {
            WinningRecordResult winningRecordResult = new WinningRecordResult();
            winningRecordResult.setWinnerId(winningRecordDTO.getWinnerId());
            winningRecordResult.setWinnerName(winningRecordDTO.getWinnerName());
            winningRecordResult.setPrizeName(winningRecordDTO.getPrizeName());
            winningRecordResult.setPrizeTier(ActivityPrizeTiersEnum.checkForName(winningRecordDTO.getPrizeTier()).getMessage());
            winningRecordResult.setWinningTime(winningRecordDTO.getWinningTime());
            winningRecordResults.add(winningRecordResult);
        }
        return winningRecordResults;
    }
}